package br.com.onbrain.bancodedados;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ObrasDAO {

	private Context ctx;
	private String table_name = "obras";
	private String[] colunas = new String[]{"_id","nome"};
	
	public ObrasDAO(Context ctx){
		this.ctx = ctx;
	}

	//INSERIR OBRA
	public boolean insert(ObrasVO nome) {
		SQLiteDatabase db = new DBHelper(ctx).getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("nome", nome.getNome());
		return (db.insert(table_name, null, values) > 0);
	}
		
	//LISTAR OBRAS
	public List<String> lista() {

		List<String> lista = new ArrayList<String>();
		SQLiteDatabase db = new DBHelper(ctx).getWritableDatabase();
		Cursor cursor = db.query(table_name, colunas, null, null, null, null,"_id");
		cursor.moveToFirst();
		do {
			String dados = "";
			dados = (cursor.getString(cursor.getColumnIndex("nome")));
			lista.add(dados);
		} while (cursor.moveToNext());

		return lista;
	}
		
	//DELETAR OBRAS
	public int deletar() {
		SQLiteDatabase db = new DBHelper(ctx).getWritableDatabase();
		Cursor cursor = db.query(table_name, colunas, null, null, null, null,
				"_id");
		while (cursor.moveToNext()) {
			db.delete("obras", "_id=?",
					new String[] { String.valueOf(cursor.getInt(0)) });
		}
		return cursor.getCount();
	}

	//VERIFICA BANCO VAZIO
	public int verificaBancoVazio() {
		SQLiteDatabase db = new DBHelper(ctx).getWritableDatabase();

		Cursor cursor = db.query(table_name, colunas, null, null, null, null,
				null);
		int cont = cursor.getCount();
		return cont;

	}
}
